﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
    public static class Utils
    {
        public static HashSet<string> LIST_TUTIENGVIET = new HashSet<string>();

        public static void GetResource(string path)
        {
            StreamReader sr = new StreamReader(path + "/tv.txt");
            string line = "";
            while ((line = sr.ReadLine()) != null)
            {
                LIST_TUTIENGVIET.Add(line);
            }
        }
        public static string GetTempFile(string extension)
        {
            // Uses the Combine, GetTempPath, ChangeExtension, 
            // and GetRandomFile methods of Path to 
            // create a temp file of the extension we're looking for. 
            return Path.Combine(Path.GetTempPath(),
                Path.ChangeExtension(Path.GetRandomFileName(), extension));
        }

        public static int VietnameseWordScore(string word)
        {
            int score = 0;
            if (Regex.IsMatch(word, @"(?i)(w|f|z)"))
                return 0;
            if (Regex.IsMatch(word, @"(?i)(ă|â|đ|ê|ô|ơ|ư)"))
                score++;
            if (Regex.IsMatch(word, @"(?i)(á|à|ả|ã|ạ|ấ|ầ|ẩ|ẫ|ậ|ắ|ằ|ẳ|ẵ|ặ|ó|ò|ỏ|õ|ọ|ố|ồ|ổ|ỗ|ộ|ớ|ờ|ở|ỡ|ợ|ú|ù|ủ|ũ|ụ|ứ|ừ|ử|ữ|ự|é|è|ẻ|ẽ|ẹ|ế|ề|ể|ễ|ệ)"))
                score++;
            return score;
        }

        public static string SplitVietnamese(string input)
        {
            string w1, w2, w3, w4;
            bool fl = false;
            for (int i = 1; i < input.Length - 1; i++)
            {
                w1 = input.Substring(0, i);
                w2 = input.Substring(i, input.Length - i);
                if (LIST_TUTIENGVIET.Contains(w1.ToLower()) && LIST_TUTIENGVIET.Contains(w2.ToLower()))
                {
                    fl = true;
                    if (VietnameseWordScore(w1) + VietnameseWordScore(w2)!=0)
                        return w1 + " " + w2;
                }
            }
            if (!fl)
            {
                for (int i = 1; i < input.Length - 1; i++)
                {
                    for (int j = i + 1; j < input.Length; j++)
                    {
                        w1 = input.Substring(0, i);
                        w2 = input.Substring(i, j - i);
                        w3 = input.Substring(j, input.Length - j);
                        if (LIST_TUTIENGVIET.Contains(w1.ToLower()) && LIST_TUTIENGVIET.Contains(w2.ToLower()) && LIST_TUTIENGVIET.Contains(w3.ToLower()))
                        {
                            fl = true;
                            if (VietnameseWordScore(w1) + VietnameseWordScore(w2) +VietnameseWordScore(w3) != 0)
                                return w1 + " " + w2 + " " + w3;
                        }
                    }
                }
            }
            return input;
        }
        public static void SendMail(string from,string user,string pass,string[] to,string subject,string body,string[] attacts=null)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress(from);
            foreach (var item in to)
                mail.To.Add(item);
            mail.Subject = subject;
            mail.Body = body;
            if(attacts!=null)
            {
                System.Net.Mail.Attachment attachment;
                foreach (var item in attacts)
                {
                    attachment = new System.Net.Mail.Attachment(item);
                    mail.Attachments.Add(attachment);
                }
            }

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(user, pass);
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

        }
        public static string ConvertToUnSign(string text)
        {
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }
        public static long _xauChungKhongKe<T>(T[] tt1, T[] tt2, Func<T, T, bool> compare = null)
        {
            long max = Math.Max(tt1.Length, tt2.Length);
            long[,] c = new long[max + 2, max + 2];

            for (long i = 0; i < tt1.Length; i++)
                for (long j = 0; j < tt2.Length; j++)
                {
                    if (compare == null)
                        if (tt1[i].Equals(tt2[j]))
                            c[i + 1, j + 1] = c[i, j] + 1;
                        else
                            c[i + 1, j + 1] = c[i + 1, j] >= c[i, j + 1] ? c[i + 1, j] : c[i, j + 1];
                    else
                        if (compare(tt1[i], tt2[j]))
                            c[i + 1, j + 1] = c[i, j] + 1;
                        else
                            c[i + 1, j + 1] = Math.Max(c[i + 1, j], c[i, j + 1]);

                }

            return c[tt1.Length, tt2.Length];
        }
        public static long _xauChungKe<T>(T[] tt1, T[] tt2, Func<T, T, bool> compare = null, long start = 0, long end = 0)
        {
            long max;
            if (start == 0 && end == 0)
                max = tt1.Length >= tt2.Length ? tt1.Length : tt2.Length;
            else
                max = end;
            long[,] c = new long[max + 2, max + 2];
            max = 0;
            for (long i = start; i < (end != 0 ? end : tt1.Length); i++)
                for (long j = start; j < (end != 0 ? end : tt2.Length); j++)
                {
                    if (compare == null)
                    {
                        if (tt1[i].Equals(tt2[j])) c[i + 1, j + 1] = c[i, j] + 1;
                        else
                            c[i + 1, j + 1] = 0;
                        if (c[i + 1, j + 1] > max) max = c[i + 1, j + 1];
                    }
                    else
                    {
                        if (compare(tt1[i], tt2[j])) c[i + 1, j + 1] = c[i, j] + 1;
                        else
                            c[i + 1, j + 1] = 0;
                        if (c[i + 1, j + 1] > max) max = c[i + 1, j + 1];
                    }

                }
            return max;
        }
    }
}
